// JavaScript Document
var thisForm;

function tb_getMozSelection(txtarea) {
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		if (selEnd==1 || selEnd==2) selEnd=selLength;
		return (txtarea.value).substring(selStart, selEnd);
	}
	
function tb_mozWrap(txtarea, lft, rgt) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + lft + s2 + rgt + s3;
}

function tb_mozReplace(txtarea,txt) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + txt + s3;
}

/*---- ie functions */
function ie_storeCaret (txtarea) {
    txtarea.caretPos = document.selection.createRange().duplicate();
}
/*
function ie_insertAtCaret (txtarea, text) {
    var caretPos = txtarea.caretPos;
    caretPos.text = text;
}
*/
function tb_IEWrap(txtarea,lft, rgt) {
	strSelection=txtarea.caretPos.text;
    txtarea.caretPos.text = lft + strSelection + rgt;
}

function tb_getIESelection(txtarea) {
    return txtarea.caretPos.text;
}

function tb_IEReplace(txtarea,txt) {
    var caretPos = txtarea.caretPos;
    caretPos.text = txt;
}

/*-----generic calls */

function tb_getSelection(txtarea) {
	if (document.all) {return tb_getIESelection(txtarea);}
	else if (document.getElementById) {return tb_getMozSelection(txtarea)}
}

function tb_wrapSelection(txtarea, lft, rgt) {
	if (document.all) {tb_IEWrap(txtarea,lft, rgt);}
	else if (document.getElementById) {tb_mozWrap(txtarea, lft, rgt);}
}

function tb_replaceSelection(txtarea,txt) {
	if (document.all) {tb_IEReplace(txtarea,txt);}
	else if (document.getElementById) {tb_mozReplace(txtarea,txt);}
}	


jQuery.fn.toolbar = function(options) {

   var container = this;
   
   var settings = jQuery.extend({
     buttons: [
        {name:'italic',desc:'mettre en italique',lft:'::',rgt:'::' },
        {name:'bold',desc:'mettre en italique',lft:'**',rgt:'**' },
        {name:'underline',desc:'mettre en italique',lft:'__',rgt:'__' },
        {name:'separ'},
        {name:'h1',desc:'titre',lft:'\\n!',rgt:'' },
        {name:'h2',desc:'sous-titre',lft:'\\n!!',rgt:'' },
        {name:'h3',desc:'sous-titre 2',lft:'\\n!!!',rgt:'' }
     ],
     buttonClass: 'button',
     mainClass: 'toolbar',
     separ: 3
   }, options);
   
   container.before("<div class='"+settings.mainClass+"' ></div>");
   var tbar=container.prev('.toolbar');
   textarealink=container.parent('form').attr("name")+"."+container.attr("name");
   $(container).focus();
  if($.browser.msie) {
  		$(container).select( function(){ ie_storeCaret(this);});
  		$(container).click( function(){ ie_storeCaret(this);});
  		$(container).keyup( function(){ ie_storeCaret(this);});
  }
   for(var i = 0; i < settings.buttons.length; i++) {
      button=settings.buttons[i];
      if (button.name=='separ') {
        html='<div style="float:left;margin-right:'+settings.separ+'px" >&nbsp;</div>';
      } else {
          if (button.fn!=undefined) {
                html="<a href=\"#\" onclick=\""+button.fn+"();\" class=\""+settings.buttonClass+"\" >";
          } else if (button.lft!=undefined) {      
                html="<a href=\"#\" onclick=\"tb_wrapSelection(document."+textarealink+",'"+button.lft+"','"+button.rgt+"');\" class=\""+settings.buttonClass+"\" >";
          }
          if (button.img!=undefined) {
                html+="<img src=\""+button.name+"\" /></a>";
          } else {
                html+=button.name+"</a>";
          }
      }
      tbar.append(html);
   }

   return this;
};
